package com.aaa.config;

import com.aaa.commen.entity.MyRedisProperties;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;


/**
 * @PackageName: com.wjw.config
 * @ClassName: MyRedisConfig
 * @Description:
 * @author: 王嘉威
 * @Date: 2021/1/12 19:36
 */
@Configuration
public class MyRedisConfig {
    /**
     * 加载配置文件到配置类中
     * @param environment
     * @return
     */
    @Bean
    public MyRedisProperties myRedisProperties(Environment environment){
        MyRedisProperties myRedisProperties = Binder.get(environment).bind(MyRedisProperties.PREFIX, MyRedisProperties.class).get();
        return myRedisProperties;
    }


    /**
     * 连接工程 用来获取redis连接对象
     * @param redisStandaloneConfiguration   单机redis配置
     * @param jedisClientConfiguration       jedis的配置
     * @return
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration,JedisClientConfiguration jedisClientConfiguration){
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
        return jedisConnectionFactory;
    }


    /**
     * redis单机配置
     * @return
     */
    @Bean
    public RedisStandaloneConfiguration redisStandaloneConfiguration(MyRedisProperties myRedisProperties){
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(myRedisProperties.getHost());
        redisStandaloneConfiguration.setPort(myRedisProperties.getPort());
        redisStandaloneConfiguration.setPassword(myRedisProperties.getPassword());
        redisStandaloneConfiguration.setDatabase(myRedisProperties.getDatabase());
        return redisStandaloneConfiguration;
    }


    /**
     * jedis配置
     * @return
     */
    @Bean
    public JedisClientConfiguration jedisClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig){
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolingClientConfigurationBuilder = builder.poolConfig(genericObjectPoolConfig);
        JedisClientConfiguration build = jedisPoolingClientConfigurationBuilder.build();
        return build;
    }


    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig(MyRedisProperties myRedisProperties){
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();

        genericObjectPoolConfig.setMaxWaitMillis(myRedisProperties.getMaxWaitMillis());
        genericObjectPoolConfig.setMaxTotal(myRedisProperties.getMaxTotal());
        genericObjectPoolConfig.setMaxIdle(myRedisProperties.getMaxIdle());
        genericObjectPoolConfig.setMinIdle(myRedisProperties.getMinIdle());
        genericObjectPoolConfig.setEvictorShutdownTimeoutMillis(myRedisProperties.getTimeout());

        return genericObjectPoolConfig;
    }
}
